package service

import (
	"fmt"
	"hanyun-admin/src/entity"
	"log"
)

//数据列表
func SelectDictTypeList(dictType entity.DictType)[]entity.DictType{
	dictTypeList := make([]entity.DictType,0)
	sql := DbEngine.Select("*")
	if dictType.DictName != "" {
		sql.Where("dict_name like ?","%"+dictType.DictName+"%")
	}
	if dictType.DictType != "" {
		sql.Where("dict_type like ?","%"+dictType.DictType+"%")
	}
	if dictType.Status != "" {
		sql.Where("status like ?","%"+dictType.Status+"%")
	}
	err := sql.Limit(dictType.PageSize,(dictType.PageNum - 1)*dictType.PageSize).Find(&dictTypeList)
	if err != nil {
		fmt.Println("select dictType list error ",err)
	}
	return dictTypeList
}
//数据列表长度
func SelectDictTypeListCount(dictType entity.DictType)int{
	sql := DbEngine.Select("count(0)")
	if dictType.DictName != "" {
		sql.Where("dict_name like ?","%"+dictType.DictName+"%")
	}
	if dictType.DictType != "" {
		sql.Where("dict_type like ?","%"+dictType.DictType+"%")
	}
	if dictType.Status != "" {
		sql.Where("status like ?","%"+dictType.Status+"%")
	}
	count,err := sql.Count(entity.DictType{})
	if err != nil {
		fmt.Println("select dictType list size error ",err)
	}
	return int(count)
}
//查看数据详情
func SelectDictTypeByDictId(dictId int64)entity.DictType{
	var dictType entity.DictType
	_,err := DbEngine.Id(dictId).Get(&dictType)
	if err != nil {
		log.Println("select dict type by dictId error ",err)
	}
	return dictType
}
//添加保存
func InsertDictType(dictType entity.DictType)error{
	_,err := DbEngine.Insert(dictType)
	return err
}
//更新数据
func UpdateDictType(dictType entity.DictType)error{
	_,err := DbEngine.Id(dictType.DictId).Update(dictType)
	return err
}
//删除
func DeleteDictType(dictId int64)error{
	dictType := new(entity.DictType)
	_,err := DbEngine.Id(dictId).Delete(dictType)
	return err
}